package cn.qingyun.gis.modules.enterprise.util;

import cn.hutool.core.util.ObjectUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.enterprise.config.ManagerType;
import cn.qingyun.gis.modules.init.system.entity.SysRole;
import cn.qingyun.gis.modules.init.system.service.ISysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Data
@Component
public class RoleTool {
    public static final String ROLE_PROVINCE = "province_manager";
    public static final String ROLE_CITY = "city_manager";
    public static final String ROLE_DISTRICT = "district_manager";
    public static final String ROLE_GRID = "grid_manager";


    @Resource
    public void setiSysRoleService(ISysRoleService iSysRoleService) {
        RoleTool.iSysRoleService = iSysRoleService;
    }

    private static ISysRoleService iSysRoleService;


    public static SysRole getRoleByManagerType(Integer managerType) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        String roleCode = ROLE_GRID;
        if (ManagerType.PROVINCE.equals(managerType)) {
            roleCode = ROLE_PROVINCE;
        } else if (ManagerType.CITY.equals(managerType)) {
            roleCode = ROLE_CITY;
        } else if (ManagerType.COUNTY.equals(managerType)) {
            roleCode = ROLE_DISTRICT;
        }

        queryWrapper.lambda().eq(SysRole::getRoleCode, roleCode)
                .last("LIMIT 1");
        SysRole role = iSysRoleService.getOne(queryWrapper);
        if (ObjectUtil.isNotNull(role)) {
            return role;
        } else {
            throw new CommonException(String.format("请联系管理员配置系统角色[%s]", roleCode));
        }
    }

}
